#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1005
#define inf 0x1fffffff
int n, m, u, v, w, sum;
int dis[maxn];
int edge[maxn][maxn];
int vis[maxn];
void transpose()
{
    for (int i = 1; i <= n; ++i)
    {
        for (int j = i + 1; j <= n; ++j)
        {
            swap(edge[i][j], edge[j][i]);
        }
    }
}
void dij()
{
    fill(vis, vis + n + 1, 0);
    fill(dis, dis + n + 1, inf);
    dis[1] = 0;
    for (int i = 1; i <= n; ++i)
    {
        int u = -1, mindis = inf;
        for (int j = 1; j <= n; ++j)
        {
            if (vis[j] == 0 && dis[j] < mindis)
            {
                mindis = dis[j];
                u = j;
            }
        }
        if (u == -1)
            break;
        vis[u] = 1;
        for (int v = 1; v <= n; ++v)
        {
            if (vis[v] == 0)
                dis[v] = min(dis[v], dis[u] + edge[u][v]);
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    fill(edge[0], edge[0] + maxn * maxn, inf);
    for (int i = 0; i < m; ++i)
    {
        cin >> u >> v >> w;
        edge[u][v] = min(w, edge[u][v]); //判断重边:>
    }
    dij();
    for (int i = 2; i <= n; ++i)
        sum += dis[i];
    transpose();
    dij();
    for (int i = 2; i <= n; ++i)
        sum += dis[i];
    cout << sum << endl;
    return 0;
}